package com.w7vm.menuweb.facade;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.w7vm.menuweb.dao.CardapioBuffetDAO;
import com.w7vm.menuweb.dao.CardapioSelfServiceDAO;
import com.w7vm.menuweb.dao.ComposicaoCardBuffetDAO;
import com.w7vm.menuweb.entity.CardapioBuffetWeb;
import com.w7vm.menuweb.entity.CardapioSelfServiceWeb;
import com.w7vm.menuweb.entity.ComposicaoCardBuffetWeb;
import com.w7vm.menuweb.utility.ConstantesEJB;
import com.w7vm.menuweb.vo.CardapioVO;

/**
 * Session Bean implementation class CardapioG1FacadeImp
 */
@Stateless(name="CardapioG1EJB")
@Remote(CardapioG1Facade.class)
public class CardapioG1FacadeImp implements CardapioG1Facade {
	
	
	
	@EJB
	private CardapioBuffetDAO buffetEJB;
	private CardapioBuffetWeb buffetEntity = new CardapioBuffetWeb();  

	@EJB
	private ComposicaoCardBuffetDAO compCardBuffetEJB;

	
	@EJB
	private CardapioSelfServiceDAO selfServiceEJB;
	private CardapioSelfServiceWeb selfServiceEntity = new CardapioSelfServiceWeb();  
	ConstantesEJB constEJB = new ConstantesEJB();


    /**
     * Default constructor. 
     */
    public CardapioG1FacadeImp() {
        // TODO Auto-generated constructor stub
    }
    
    
    
  //======== :: METHODs - CARDAPIO BUFFET ::======================================================== //
	
  	@Override
  	public int saveBuffet(CardapioBuffetWeb buffet) {
  		return buffetEJB.saveCardapioBuffet(buffet);
  	}

  	@Override
  	public CardapioBuffetWeb updateBuffet(CardapioBuffetWeb buffetEntity) {
  		return buffetEJB.mergeUpdate(buffetEntity);
  	}
  	
  	@Override
  	public int updateStatusCardapioBuffet(CardapioBuffetWeb buffetEntity) {
  		return buffetEJB.alterarStatusCardapioBuffet(buffetEntity);
  	}

  	@Override
  	public void deleteBuffet(int id) {
  		buffetEJB.deleteCardapioBuffet(id);
  	} 

  	@Override
  	public List<CardapioBuffetWeb> findBuffetByIDRestauranteSede(int id) {
  		return buffetEJB.findCardapioBuffetByIDRestauranteSede(id);
  	}
  	
  	@Override
	public CardapioBuffetWeb findBuffetByID(int id){
  		System.out.println("findBuffetByID");
  		return buffetEJB.findCardapioBuffetByID(id);
  	}


  	@Override
  	public List<CardapioBuffetWeb> findBuffetByIDRestauranteFilial(int id) {
  		return buffetEJB.findCardapioBuffetByIDRestauranteFilial(id);
  	}

  	public CardapioBuffetWeb setCardapioBuffetEntity(CardapioVO cardapioVO){
   		System.out.println("[Method][setCardapioBuffetEntity]");
  		buffetEntity.setNomeBuffet(cardapioVO.getNomePrato());
  		buffetEntity.setEntradasFrias(cardapioVO.getEntradasFrias());
  		buffetEntity.setEntradasQuentes(cardapioVO.getEntradasQuentes());
  		buffetEntity.setSobremesa(cardapioVO.getSobremesa());
  		buffetEntity.setCortesia(cardapioVO.getCortesia());
  		buffetEntity.setDiasSemana(cardapioVO.getDiasSemana());
  		buffetEntity.setDtInicio(cardapioVO.getDtInicio());
  		buffetEntity.setDtCadastro(cardapioVO.getDtCadastro());

  		System.out.println("DATA TERMINO ....................."+ cardapioVO.getDtTermino());
  		
  		buffetEntity.setDtTermino(cardapioVO.getDtTermino());
  		buffetEntity.setComposicaoCardBuffetWebs(cardapioVO.getComposicaoCardBuffetL());
  		
		Date nowData = new Date();
		SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
		String dateToday = DATE_FORMAT.format(nowData);
		String dateCard = DATE_FORMAT.format(cardapioVO.getDtInicio());
  		
		System.out.println("DATA NOW....................."+ dateToday );
		
		System.out.println("DATA CARDAPIO....................."+ dateCard );
		
		if(dateToday.equals(dateCard)){
			System.out.println("DATA CARDAPIO ATIVAÇÃO");
			buffetEntity.setStatus(constEJB.ATIVO);
  		}
  		else if(cardapioVO.getStatus() == constEJB.AGENDADO_ATIVACAOs){
  	  		buffetEntity.setStatus(constEJB.AGENDADO_ATIVACAO);
  	  	}
  	  	else if(cardapioVO.getStatus() == constEJB.ATIVOs){
  	  			buffetEntity.setStatus(constEJB.ATIVO);
  		}
		return buffetEntity;
  	}
  	
  	public CardapioVO setCardapioBuffetVO(CardapioBuffetWeb buffetEntity){
  		CardapioVO cardapioVO = new CardapioVO();
  		cardapioVO.setIdCardapioBuffetWeb(buffetEntity.getIdCardapioBuffetWeb());
  		cardapioVO.setNomePrato(buffetEntity.getNomeBuffet());
  		cardapioVO.setEntradasFrias(buffetEntity.getEntradasFrias());
  		cardapioVO.setEntradasQuentes(buffetEntity.getEntradasQuentes());
  		cardapioVO.setSobremesa(buffetEntity.getSobremesa());
  		
  		DateFormat dtFormat = new SimpleDateFormat("dd/MM/yyyy");
  		String dateInicio = dtFormat.format(buffetEntity.getDtInicio());
  		
  		System.out.println("DATA CARDAPIO INICIO............"+ dateInicio);
  		
  		String dateFim = dtFormat.format(buffetEntity.getDtTermino());
  		
  		try {
			cardapioVO.setDtInicio(dtFormat.parse(dateInicio));
	  		cardapioVO.setDtTermino(dtFormat.parse(dateFim));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  		
  		cardapioVO.setCortesia(buffetEntity.getCortesia());
  		cardapioVO.setDtCadastro(buffetEntity.getDtCadastro());
  		cardapioVO.setDiasSemana(buffetEntity.getDiasSemana());
  		
  		if(buffetEntity.getStatus() == constEJB.AGENDADO_ATIVACAO){
  			cardapioVO.setStatus(constEJB.AGENDADO_ATIVACAOs);
  		}
  		else if(buffetEntity.getStatus() == constEJB.ATIVO){
  			cardapioVO.setStatus(constEJB.ATIVOs);
  		}
  		else if(buffetEntity.getStatus() == constEJB.INATIVO){
  			cardapioVO.setStatus(constEJB.INATIVOs);
  		}
  		return cardapioVO;
  	}
  	
	@Override
	public void deleteComposicaoCardapioBuffet(List<ComposicaoCardBuffetWeb> compCardBuffetEntity) {
		compCardBuffetEJB.deleteCardapioBuffet(compCardBuffetEntity);
	}
	
	@Override
	public Long buscaQtdeCardapioBuffetAtivo(int idRestaurante, String tipo) {
		return buffetEJB.countCardapioBuffetAtivo(idRestaurante, tipo);
	}
	
  	
  //======== :: METHODs - CARDAPIO SELF SERVICE ::======================================================== //

	@Override
	public Long buscaQtdeCardapioSelfServiceAtivo(int idRestaurante, String tipo) {
		return selfServiceEJB.countCardapioSelfServiceAtivo(idRestaurante, tipo);
	}
	
  	@Override
  	public int updateStatusCardapioSelfService(CardapioSelfServiceWeb selfServiceEntity) {
  		return selfServiceEJB.alterarStatusCardapioSelfService(selfServiceEntity);
  	}

	
  	@Override
  	public int saveSelfService(CardapioSelfServiceWeb selfServiceEntity) {
  		return selfServiceEJB.saveCardapioSelfService(selfServiceEntity);
  	}

  	@Override
  	public CardapioSelfServiceWeb updateSelfService(CardapioSelfServiceWeb selfServiceEntity) {
  		return selfServiceEJB.mergeUpdate(selfServiceEntity);
  	}

  	@Override
  	public void deleteSelfService(int id) {
  		selfServiceEJB.deleteCardapioSelfService(id);
  	}

  	@Override
	public CardapioSelfServiceWeb findSelfServiceByID(int id){
  		return selfServiceEJB.findCardapioSelfServiceByID(id);
	}
  	
  	@Override
  	public List<CardapioSelfServiceWeb> findSelfServiceByIDRestauranteSede(int id) {
  		return selfServiceEJB.findCardapioSelfServiceByIDRestauranteSede(id);
  	}

  	@Override
  	public List<CardapioSelfServiceWeb> findSelfServiceByIDRestauranteFilial(int id) {
  		return selfServiceEJB.findCardapioSelfServiceByIDRestauranteFilial(id);
  	}

  	@Override
  	public CardapioSelfServiceWeb setCardapioSelfServiceEntity(CardapioVO cardapioVO){
   		selfServiceEntity.setNomeSelfService(cardapioVO.getNomePrato());
   		selfServiceEntity.setEntradasFrias(cardapioVO.getEntradasFrias());
   		selfServiceEntity.setEntradasQuentes(cardapioVO.getEntradasQuentes());
   		selfServiceEntity.setSobremesa(cardapioVO.getSobremesa());
   		selfServiceEntity.setCortesia(cardapioVO.getCortesia());
   		selfServiceEntity.setDiasSemana(cardapioVO.getDiasSemana());
   		selfServiceEntity.setDtInicio(cardapioVO.getDtInicio());
   		selfServiceEntity.setDtTermino(cardapioVO.getDtTermino());
   		selfServiceEntity.setComposicaoCardSelfServiceWebs(cardapioVO.getComposicaoCardSelfServiceL());
  		
		Date nowData = new Date();
		SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
		String dateToday = DATE_FORMAT.format(nowData);
		String dateCard = DATE_FORMAT.format(cardapioVO.getDtInicio());

   		selfServiceEntity.setDtCadastro(nowData);

		
		if(dateToday.equals(dateCard)){
			selfServiceEntity.setStatus(constEJB.ATIVO);
  		}
  		else{
  			selfServiceEntity.setStatus(constEJB.AGENDADO_ATIVACAO);
  	  	}
  		return selfServiceEntity;
  	}
  	
  	@Override
  	public CardapioVO setCardapioSelfServiceVO(CardapioSelfServiceWeb selfServiceEntity){
  		
  		CardapioVO cardapioVO = new CardapioVO();
  		cardapioVO.setIdCardapioSelfServiceWeb(selfServiceEntity.getIdCardapioSelfServiceWeb());

  		System.out.println("ID SelfService.........................................."+ cardapioVO.getIdCardapioSelfServiceWeb());
  		
  		cardapioVO.setNomePrato(selfServiceEntity.getNomeSelfService());
  		cardapioVO.setEntradasFrias(selfServiceEntity.getEntradasFrias());
  		cardapioVO.setEntradasQuentes(selfServiceEntity.getEntradasQuentes());
  		cardapioVO.setSobremesa(selfServiceEntity.getSobremesa());
  		
  		SimpleDateFormat dtFormat = new SimpleDateFormat("dd/MM/yyyy", new Locale("pt","br"));
  		
  		System.out.println("DATA.........................................."+ selfServiceEntity.getDtInicio());
  		System.out.println("DATA FORMATADA.........................................."+ dtFormat.format(selfServiceEntity.getDtInicio()));
  		
  		String dateInicio = dtFormat.format(selfServiceEntity.getDtInicio());

  		String dateFim = dtFormat.format(selfServiceEntity.getDtTermino());
  		
  		try {
  	  		System.out.println("DATA FORMATADA II.........................................."+ dtFormat.parse(dateInicio));

  			cardapioVO.setDtInicio(selfServiceEntity.getDtInicio());
	  		cardapioVO.setDtTermino(selfServiceEntity.getDtTermino());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  		
  		cardapioVO.setCortesia(selfServiceEntity.getCortesia());
  		cardapioVO.setDtCadastro(selfServiceEntity.getDtCadastro());
  		cardapioVO.setDiasSemana(selfServiceEntity.getDiasSemana());
  		
  		if(selfServiceEntity.getStatus() == constEJB.AGENDADO_ATIVACAO){
  			cardapioVO.setStatus(constEJB.AGENDADO_ATIVACAOs);
  		}
  		else if(selfServiceEntity.getStatus() == constEJB.ATIVO){
  			cardapioVO.setStatus(constEJB.ATIVOs);
  		}
  		else if(selfServiceEntity.getStatus() == constEJB.INATIVO){
  			cardapioVO.setStatus(constEJB.INATIVOs);
  		}
  		return cardapioVO;
  	}





}
